package com.ruoyi.project.pro.upload.controller;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.pro.picture.domain.ProPicture;
import com.ruoyi.project.pro.picture.service.IProPictureService;
import com.ruoyi.project.pro.upload.FileUpLoadUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @program: zhizun
 * @author: Mrs.ye
 * @create: 2020-09-16 20:38
 * @description:
 * 产品系列管理控制器
 **/

@Controller
@RequestMapping("/pro/upload")
public class ProUploadController extends BaseController {

  @Autowired
  private IProPictureService proPictureService;


    private String prefix = "pro/upload";

    /**
     *@return 跳转到展示产品系列的html页面
     */
    @RequiresPermissions("pro:upload:view")
    @GetMapping()
    public String content() {
        return prefix + "/upload";
    }


    @RequiresPermissions("pro:upload:add")
    @PostMapping("/addPicture")
    @ResponseBody
    public AjaxResult add(MultipartFile file_data) {


        String fileName = FileUpLoadUtil.upLoadFile(file_data);

        System.out.println("上传成功");
        ProPicture  proPicture=new ProPicture();
        proPicture.setPicturePath("https://tcr-5cb5kifq-1302869269.cos.ap-guangzhou.myqcloud.com/"+fileName);
        proPicture.setPictureName(fileName);
        return toAjax(proPictureService.insertPicture(proPicture));
    }



}
